package com.dragon.cloud.dragon.demo.jooq.controller;

import com.dragon.cloud.common.base.R;
import com.dragon.cloud.common.util.idgenerator.IdWorker;
import com.dragon.cloud.dragon.demo.jooq.db.tables.RoleUserUuid;
import com.dragon.cloud.dragon.demo.jooq.db.tables.RolesUuid;
import com.dragon.cloud.dragon.demo.jooq.db.tables.UsersUuid;
import com.dragon.cloud.dragon.demo.jooq.db.tables.records.*;
import lombok.extern.slf4j.Slf4j;
import org.jooq.DSLContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Slf4j
@RequestMapping
@RestController
public class TestController {
    @Autowired
    DSLContext create;

    @GetMapping("insert")
    public R insert() {
        for (int a = 0; a < 100; a++) {
            System.out.println(a);
            List<UsersRecord> usersRecords = new ArrayList<>(10000);
            List<RoleUserRecord> roleUserRecords = new ArrayList<>(10000);
            List<RolesRecord> rolesRecords = new ArrayList<>(10000);
            for (var i = 0; i < 10000; i++) {
                UsersRecord usersRecord = new UsersRecord();
                usersRecord.setId(IdWorker.getId());
                usersRecord.setUsername("tom");
                usersRecord.setPassword("123");
                usersRecord.setEnabled((byte) 1);
                usersRecords.add(usersRecord);
                RolesRecord rolesRecord = new RolesRecord();
                rolesRecord.setId(IdWorker.getId());
                rolesRecord.setRole("role_name");
                rolesRecord.setUsername("tom");
                rolesRecords.add(rolesRecord);
                RoleUserRecord roleUserRecord = new RoleUserRecord();
                roleUserRecord.setRoleId(rolesRecord.getId());
                roleUserRecord.setUserId(usersRecord.getId());
                roleUserRecords.add(roleUserRecord);
            }
            Long userBegin = System.currentTimeMillis();
            create.batchInsert(usersRecords).execute();
            log.info("user  insert耗时: " + (System.currentTimeMillis() - userBegin));

            Long roleBegin = System.currentTimeMillis();
            create.batchInsert(rolesRecords).execute();
            log.info("role  insert耗时: " + (System.currentTimeMillis() - roleBegin));
            Long roleuserBegin = System.currentTimeMillis();
            create.batchInsert(roleUserRecords).execute();
            log.info("user_role  insert耗时: " + (System.currentTimeMillis() - roleuserBegin));
            try {
                Thread.sleep(1000 * 60 * 5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        return R.ok();
    }

    @GetMapping("insertUuid")
    public R insertUuid() {
        for (int a = 0; a < 100; a++) {
            System.out.println(a);
            List<UsersUuidRecord> usersRecords = new ArrayList<>(10000);
            List<RoleUserUuidRecord> roleUserRecords = new ArrayList<>(10000);
            List<RolesUuidRecord> rolesRecords = new ArrayList<>(10000);
            for (var i = 0; i < 10000; i++) {
                UsersUuidRecord usersRecord = new UsersUuidRecord();
                usersRecord.setId(UUID.randomUUID().toString());
                usersRecord.setUsername("tom");
                usersRecord.setPassword("123");
                usersRecord.setEnabled((byte) 1);
                usersRecords.add(usersRecord);
                RolesUuidRecord rolesRecord = new RolesUuidRecord();
                rolesRecord.setId(UUID.randomUUID().toString());
                rolesRecord.setRole("role_name");
                rolesRecord.setUsername("tom");
                rolesRecords.add(rolesRecord);
                RoleUserUuidRecord roleUserRecord = new RoleUserUuidRecord();
                roleUserRecord.setRoleId(rolesRecord.getId());
                roleUserRecord.setUserId(usersRecord.getId());
                roleUserRecords.add(roleUserRecord);
            }
            Long userBegin = System.currentTimeMillis();
            create.batchInsert(usersRecords).execute();
            log.info("user uuid insert耗时: " + (System.currentTimeMillis() - userBegin));

            Long roleBegin = System.currentTimeMillis();
            create.batchInsert(rolesRecords).execute();
            log.info("role uuid insert耗时: " + (System.currentTimeMillis() - roleBegin));
            Long roleuserBegin = System.currentTimeMillis();
            create.batchInsert(roleUserRecords).execute();
            log.info("user_role uuid insert耗时: " + (System.currentTimeMillis() - roleuserBegin));
            try {
                Thread.sleep(1000 * 60 * 5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return R.ok();
    }

    @GetMapping("test")
    public R test(@RequestParam("username") String username, @RequestParam("password") String password) {
        log.info(username);
        log.info(password);
        return R.ok("测试成功");

    }

}
